package com.example.tool.service.claimsinfo;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tool.bean.business.ClaimsInfoDO;
import com.example.tool.bean.wcaiexcel.RequestData;
import com.example.tool.erp.sdk.api.sales.dto.TradeQueryResponse;
import com.example.tool.mapper.ClaimsInfoMapper;
import com.example.tool.util.WDTUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ClaimsInfoServiceImpl extends ServiceImpl<ClaimsInfoMapper, ClaimsInfoDO> implements ClaimsInfoService {
    @Autowired
    ClaimsInfoMapper claimsInfoMapper;

    @Async
    @Override
    public void storageClaimsInfo(RequestData data) {
        String spoken = data.getSpoken();
        String rawSpoken = data.getRawSpoken();
        String receivedName = data.getReceivedName();
        String groupName = data.getGroupName();
        String groupRemark = data.getGroupRemark();
        String roomType = data.getRoomType();
        String atMe = data.getAtMe();
        List<String> contents = Arrays.asList("处理方案：理赔", "处理方案：补发", "处理方案：退差价", "处理方案：退全款");
        Boolean flag=false;
        if(!"服务登记信息同步".equals(receivedName)){
            return;
        }
//        if(!roomType.equals("3")) {
//            return;
//        }
        for (String content:contents){
            flag=spoken.contains(content);
            if(flag){break;}
        }
        if((spoken.contains("问题：物流问题/")&&!spoken.contains("物流问题/拦截/顾客问题-拦截退回"))&&!spoken.contains("物流问题/物流问题-拦截退回")&&flag){
            List<String> texts = Arrays.stream(spoken.split("；")).map(text->text.substring(text.indexOf("：")+1)).collect(Collectors.toList()).subList(1,3);
            List<String>trackNos=Arrays.stream(texts.get(1).split(",")).collect(Collectors.toList());
            String issue = texts.get(0);
            for(int i=0; i<trackNos.size();i++){
                ClaimsInfoDO claimsInfoDO=new ClaimsInfoDO();
                claimsInfoDO.setWaybillNo(trackNos.get(i));
                claimsInfoDO.setRemarks(issue);
                TradeQueryResponse.OrderItem orderItem = WDTUtil.logisticsNoQueryOrder(trackNos.get(i));
                if (ObjectUtil.isNotNull(orderItem)){
                claimsInfoDO.setShopName(orderItem.getShopName());
                if (StringUtils.isBlank(issue)){
                claimsInfoDO.setRemarks(orderItem.getCsRemark());}}
                claimsInfoDO.setCreateTime(new Date());
                claimsInfoMapper.insert(claimsInfoDO);
            }
        }
    }

    @Override
    public List<List<String>> findRemainderData() {
        List<List<String>>data=new ArrayList<>();
        List<ClaimsInfoDO>dbData=claimsInfoMapper.selectList(new QueryWrapper<>());
        for (ClaimsInfoDO item:dbData){
            data.add(Arrays.asList(item.getWaybillNo(),item.getRemarks(),Optional.ofNullable(item.getShopName()).orElse(""),item.getYyyyMMdd()));
        }
        List<Long>ids=dbData.stream().map(ClaimsInfoDO::getId).collect(Collectors.toList());
        if (!ids.isEmpty()){
        claimsInfoMapper.deleteBatchIds(ids);
        }
        return data;
    }
}
